<?php

namespace App\Http\Controllers\VideoApi;

use App\Http\Config\ErrorCode;
use App\Library\UpTools;
use App\Models\Admin\Picture;
use App\Models\video\CourseCatalogModel;
use App\Models\video\CourseModel;
use App\Models\video\VideoUserModel;
use App\Models\video\VodVideoModel;
use App\Models\video\WxServer;
use Illuminate\Http\Request;

class ShareController extends BaseController
{
//    public function __construct(Request $request)
//    {
//        $this->noCheckTokenAction = ['getMyPlayClassList', 'getPlayVideoList']; //不校验openid
//        parent::__construct($request);
//    }

    // 分享二维码图
    public function getSharePic(Request $request){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $pagePath = $request->pagePath ?: "";
        $param = $request->params ?: "0";
        $dir = "attachment/wxapp/{$request->userLoginData["id"]}/"; // 二维码路径
        $file = md5($pagePath.$param).".png";
        $res = WxServer::getInstance()->generateWxCode($param, $pagePath);
        if(isset($res["code"])){
            return $this->return_result($res);
        }
        global $scf_data;
        if(!empty($scf_data["IS_SCF"])){ // 云开发
            $object = new Picture();
            $result = $object->uploadImg($dir.$file, $res);
            $this->returnData["data"] = ["wxQrCode" => $result['ObjectURL']];
        }else{
            $newDir = base_path() . "/public/" . $dir;
            if(!file_exists($newDir.$file)){ // 写入文件
                UpTools::mkdir($newDir);
                file_put_contents($newDir.$file, $res);
            }
            if(basename($_SERVER["DOCUMENT_ROOT"]) != "public"){ // 根目录运行
                $this->returnData["data"] = ["wxQrCode" => $request->getSchemeAndHttpHost()."/public/{$dir}{$file}"];
            }else{
                $this->returnData["data"] = ["wxQrCode" => $request->getSchemeAndHttpHost()."/{$dir}{$file}"];
            }
        }
        return $this->return_result($this->returnData);
    }

    public function getGeneratePic(Request $request){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $pagePath = $request->pagePath ?: "pages/index/index";
        $param = $request->id ? "id={$request->id}" : "0";
        $dir = "attachment/wxapp/"; // 二维码路径
        $md5 = md5($pagePath.$param.$request->userLoginData["id"]);
        $file = $md5.".png";
        $data = VideoUserModel::getFields("nickname,avatar", ["id" => $request->userLoginData['id']], true, false);
        if($request->type == '2'){
            $course = CourseModel::getFields("name as title,cover_img_id", ["id" => $request->id]);
            $course["viewCount"] = CourseCatalogModel::query()->from(CourseCatalogModel::query()->getModel()->getTable()." as cc")
                ->leftJoin(VodVideoModel::query()->getModel()->getTable()." as v", "v.id", "=", "cc.video_id")
                ->where(["cc.course_id" => $request->id, "cc.status" => "1"])
                ->sum("v.view_count"); // 播放次数
            $data = array_merge($data, $course);
        }else{
            $video = VodVideoModel::getFields("title,cover_img_id,view_count", ["id" => $request->id]);
            if(empty($video)){
                return $this->return_result(ErrorCode::code("customized", "视频不存在"));
            }
            $data = array_merge($data, $video);
        }
        global $scf_data;
        if(!empty($scf_data["IS_SCF"])){ // 云开发
            $res = WxServer::getInstance()->generateWxCode($param, $pagePath);
            if(isset($res["code"])){
                return $this->return_result($res);
            }
            $object = new Picture();
            $result = $object->uploadImg($dir.$file, $res);
            $data['wxQrCode'] = $result['ObjectURL'];
        }else{
            $data['wxQrCode'] = base_path() . "/public/" . $dir . $file;
            if(!file_exists($data['wxQrCode'])){
                $res = WxServer::getInstance()->generateWxCode($param, $pagePath);
                if(isset($res["code"])){
                    return $this->return_result($res);
                }
                UpTools::mkdir(base_path() . "/public/" . $dir);
                file_put_contents($data['wxQrCode'], $res);
            }
        }
        $this->returnData["data"] = ["picUrl" => UpTools::generateShare($md5, $data)];
        return $this->return_result($this->returnData);
    }
}